<?php

namespace App\Http\Controllers\Site\Custom\Purchaser;

use App\B2BProject\Area;
use App\B2BProject\Custom\Purchaser\CounterEnquiry;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\B2BProject\Site\HelperControllerBuildTrait;

class CounterEnquiryController extends Controller
{
    use HelperControllerBuildTrait;

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if ($request->ajax()) {
            $request->query->set('page', $request->query('page', $request->page));
            $request->query->set('pageSize', $request->query('pageSize', $request->pageSize));

            $counter_enquiries = CounterEnquiry::whereHas('enquiry', function (Builder $builder) {
                $builder->where('member_id', \Auth::id());
            });

            if ($state = $request->query('state')) {
                $counter_enquiries->where('status', $state - 1);
            }

            if ($start = $request->query('time1')) {
                $counter_enquiries->where('created_at', '>=', date('Y-m-d', strtotime($start)));
            }

            if ($end = $request->query('time2')) {
                $counter_enquiries->where('created_at', '<=', date('Y-m-d', strtotime($end)));
            }

            $counter_enquiries = $counter_enquiries->orderBy('created_at',
                'desc')->paginate($request->query('pageSize'));

            foreach ($counter_enquiries as $counter_enquiry) {
                $data[] = [
                    'id'               => $counter_enquiry->id,
                    'goodsname'        => $counter_enquiry->enquiry->title,
                    'price'            => $counter_enquiry->price,
                    'amount'           => $counter_enquiry->amount,
                    'state'            => $counter_enquiry->status,
                    'counterofferdate' => date("Y-m-d H:i:s", strtotime($counter_enquiry->created_at)),
                    'district'         => Area::where('id',
                            $counter_enquiry->province_id)->first()->name .
                                          ' ' .
                                          Area::whereId($counter_enquiry->city_id)->first()->name,
                    'personname'       => $counter_enquiry->contact_name,
                    'tel'              => $counter_enquiry->contact_phone,
                ];
            }
            $responseText['data']           = $data;
            $responseText['pagingConfData'] = [
                'page'    => $counter_enquiries->currentPage(),
                'perpage' => $request->query('pageSize'),
                'total'   => $counter_enquiries->total(),
            ];

            return response()->json($responseText);
        }

        return view('host/custom/purchaser/counterenquiry/index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int                      $id
     *
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    // 处理
    public function status(Request $request)
    {
        $member = \Auth::user();
        if ($request->ajax()) {
            $id = $request->id;
            CounterEnquiry::findOrFail($id)->update(['status' => 0]);

            return $this->index($request);
        }
    }

    // 删除
    public function cancel(Request $request)
    {
        if ($request->ajax()) {
            $id = $request->id;
            CounterEnquiry::findOrFail($id)->delete();

            return $this->index($request);
        }
    }
}
